package com.zzl.bonds.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.bonds.ticket.constant.type.CirculationEnterpriseFlagStatusEnum;
import com.zzl.bonds.ticket.dao.TicketCirculationEnterpriseDao;
import com.zzl.bonds.ticket.dto.TickecirculationErnterpriseAddDto;
import com.zzl.bonds.ticket.dto.TickecirculationErnterpriseUpdateDto;
import com.zzl.bonds.ticket.entity.TicketCirculationEnterpriseEntity;
import com.zzl.bonds.ticket.entity.TicketEntity;
import com.zzl.bonds.ticket.service.TicketCirculationEnterpriseService;
import com.zzl.bonds.ticket.service.TicketService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 票据流转企业
 *
 * @author gull
 * @date 2022-07-06 13:43:52
 */
@Service("ticketCirculationEnterpriseService")
public class TicketCirculationEnterpriseServiceImpl extends ServiceImpl<TicketCirculationEnterpriseDao, TicketCirculationEnterpriseEntity> implements TicketCirculationEnterpriseService {
    @Autowired
    private TicketService ticketService;


    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TicketCirculationEnterpriseEntity> page = this.page(
                new Query<TicketCirculationEnterpriseEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param tickecirculationErnterpriseAddDto 新增实体
     * @param user                              当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveTickecirculationErnterprise(TickecirculationErnterpriseAddDto tickecirculationErnterpriseAddDto, User user) throws SysException {
        TicketCirculationEnterpriseEntity ticketCirculationEnterpriseEntity = new TicketCirculationEnterpriseEntity();
        BeanUtils.copyProperties(tickecirculationErnterpriseAddDto, ticketCirculationEnterpriseEntity);
        ticketCirculationEnterpriseEntity.setCreatorId(user.getId());
        if (!this.save(ticketCirculationEnterpriseEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param tickecirculationErnterpriseUpdateDto 修改实体
     * @param user                                 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateTickecirculationErnterpriseById(TickecirculationErnterpriseUpdateDto tickecirculationErnterpriseUpdateDto, User user) throws SysException {
        TicketCirculationEnterpriseEntity ticketCirculationEnterpriseEntity = new TicketCirculationEnterpriseEntity();
        BeanUtils.copyProperties(tickecirculationErnterpriseUpdateDto, ticketCirculationEnterpriseEntity);
        ticketCirculationEnterpriseEntity.setUpdaterId(user.getId());
        if (!this.updateById(ticketCirculationEnterpriseEntity)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

    @Override
    public TicketCirculationEnterpriseEntity getByTicketOpenIdAndCirculationEnterpriseId(String ticketOpenId, String circulationEnterpriseId) {
        QueryWrapper<TicketCirculationEnterpriseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TicketCirculationEnterpriseEntity::getTicketOpenId, ticketOpenId)
                .eq(TicketCirculationEnterpriseEntity::getCirculationEnterpriseId, circulationEnterpriseId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<String> getAcquirerDropDownList(String ticketId) {
        TicketEntity byId = ticketService.getById(ticketId);
        if (byId.getCirculationEnterpriseFlag().equals(CirculationEnterpriseFlagStatusEnum.NO.getCode())) {
            return null;
        } else {
            QueryWrapper<TicketCirculationEnterpriseEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TicketCirculationEnterpriseEntity::getTicketOpenId, byId.getTicketOpenId());
            List<TicketCirculationEnterpriseEntity> ticketCirculationEnterpriseEntities = baseMapper.selectList(queryWrapper);
           if(CollectionUtils.isEmpty(ticketCirculationEnterpriseEntities)){
               return new ArrayList<>();
           }else {
               return ticketCirculationEnterpriseEntities.stream().map(TicketCirculationEnterpriseEntity::getCirculationEnterpriseId)
                       .distinct().collect(Collectors.toList());
           }
        }
    }

}